 (function(){
	var obj={};
	//用来设置ajax回调函数的上下文。让回调函数中的this指向这个对象。
	obj.ajax = function(type, url, data, headers, context, callback){
		$.ajax({
			type: type,
			url: url,
			data: JSON.stringify(data),
			headers: headers,
			context:context
		}).done(callback);
	};
	   window.ajaxRequest = obj;
    })();
    
    /*页面初始化*/
    $(function(){
    	/* $("#ad_form").validate({  
             rules : {
                 adPrice : "required",  //用于控制用户名输入框必填  
                 min:0,
                 date:"required"
   
             },   
             messages : {  
            	 adPrice : "请输入用户名",  
                 min:"请输入合理价格",
                 date:"时间不能为空"
             }  
   
         });  */
    	
    	$("#show_tip").click(function(e){
    	     e.preventDefault();
    	     if($("#price").val() != "" && $("#date").val() != ""){
    	     $("#miss-info-alert").addClass("alert-failed");
    	     swal({
    	         title: "Are you sure?",
    	         text: "You will buy the advertisement!",
    	         type: "info",
    	         showCancelButton: true,
    	         confirmButtonColor: "#AEDEF4",
    	         confirmButtonText: "Yes, put it!",
    	         cancelButtonText: "No, cancel !",
    	         closeOnConfirm: false,
    	         closeOnCancel: false
    	       },
    	       function(isConfirm){
    	         if (isConfirm) {
    	       var formData = new FormData($("form").get(0));
    	               $.ajax({
    	                   url: "/spring-merchant/advertisement",
    	                   type: "post",
    	                   data: formData,
    	                   contentType: false,
    	                   processData: false
    	               }).done(function (msg) {
    	                    swal("buy", "Your have put your request.", "success");
    	                    $("#price").val("");
    	                    $("#date").val("");
    	                    $("#time").prop('selectedIndex', 0).attr("selected","selected");
    	                    $("#foodList").prop('selectedIndex', 0).attr("selected","selected");
    	               });
    	         } else {
    	           swal("Cancelled", "You have cancle the operation)", "error");
    	            $("#price").val("");
    	                $("#date").val("");
    	                $("#time").prop('selectedIndex', 0).attr("selected","selected");
    	                $("#foodList").prop('selectedIndex', 0).attr("selected","selected");
    	         }
    	       });
    	     }
    	     else{
    	     $("#miss-info-alert").removeClass("alert-failed");
    	     }
    	    });
    	
    	function getzf(num){  
            if(parseInt(num) < 10){  
                num = '0'+num;  
            }  
            return num;  
        }
    	
 	   function getMyDate(str){  
            var oDate = new Date(str),  
            oYear = oDate.getFullYear(),  
            oMonth = oDate.getMonth()+1,  
            oDay = oDate.getDate(),  
            oHour = oDate.getHours(),  
            oMin = oDate.getMinutes(),  
            oSen = oDate.getSeconds(),  
            oTime = oYear +'-'+ getzf(oMonth) +'-'+ getzf(oDay) +' '+ getzf(oHour) +':'+ getzf(oMin) +':'+getzf(oSen);//最后拼接时间  
            return oTime;  
        };
    	
    	 ajaxRequest.ajax("GET","/spring-merchant/foods?food_type_id=all",null,null,this,function(foods){
	   			console.log(foods);
    		 	foods.forEach(function(item){
		    		 $("#foodList").append($("<option>").attr("value",item.id).text(item.name));
	   			});
    	 });
     
    	  $("li[name=operChange]").on("click", function(){
              $(this).siblings().filter(".active").removeClass("active");
              $(this).addClass("active");
          });
    	  
    	    $("#show_success").on("click",function(){
    	    	  $("#history").addClass("not_display");
        		  $("#send-form-div").addClass("not_display");
        		  $("#success").removeClass("not_display");
    	    });
    	  
    	  $("#show_advertising").on("click",function(){
	    	  $("#history").addClass("not_display");
    		  $("#send-form-div").removeClass("not_display");
    		  $("#success").addClass("not_display");
	    });
    	  
    	  var index=0;
    	  
    	  $("#show_history").on("click",function(){
    		  index = 0;
    		  $("#send-form-div").addClass("not_display");
    		  $("#history").removeClass("not_display");
    		  $("#success").addClass("not_display");
    		  ajaxRequest.ajax("GET","/spring-merchant/views/advertisement/all",null,null,this,function(advertisement){
    			  $("#history_table").empty();
    			  var th = $("<thead>");
    			  var tr = $("<tr>");
    			//  <th  data-field="index" data-formatter="indexFormatter" >序号</th> 
    			  	 tr.append($("<th>").text("num"));
    			  	 tr.append($("<th>").text("adPrice"));
	 			 	 tr.append($("<th>").text("foodName"));
	 			 	 tr.append($("<th>").text("foodPrice"));
	 			 	 tr.append($("<th>").text("startTime"));
	 			 	 tr.append($("<th>").text("endTime"));
	 			 	 tr.append($("<th>").text("status"));
	 			 	 th.append(tr);
	 			 	 $("#history_table").append(th);
	 			 	 var tb = $("<tbody>");
    			  advertisement.forEach(function(item){
					var tr = $("<tr>");
					tr.append($("<td>").text(++index));
    				 tr.append($("<td>").text(item.adPrice));
    			 	 tr.append($("<td>").text(item.foodName));
    			 	 tr.append($("<td>").text(item.foodPrice));
    			 	 tr.append($("<td>").text(getMyDate(item.startTime)));
    			 	 tr.append($("<td>").text(getMyDate(item.endTime)));
    			 	 tr.append($("<td>").text(item.status));
    			 	 tb.append(tr);
    			 	 $("#history_table").append(tb);
    			  })
    		  })
    	  });
    	  
    	  $("#show_success").on("click",function(){
    		  index = 0;
    		  $("#send-form-div").addClass("not_display");
    		  $("#history").addClass("not_display");
    		  $("#success").removeClass("not_display");
    		  ajaxRequest.ajax("GET","/spring-merchant/views/advertisement/success",null,null,this,function(advertisement){
    			  console.log(advertisement);
    			  $("#success_table").empty();
    			  var th = $("<thead>");
    			  var tr = $("<tr>");
 			  	     tr.append($("<th>").text("num"));
	 				 tr.append($("<th>").text("adPrice"));
	 			 	 tr.append($("<th>").text("foodName"));
	 			 	 tr.append($("<th>").text("foodPrice"));
	 			 	 tr.append($("<th>").text("startTime"));
	 			 	 tr.append($("<th>").text("endTime"));
	 			 	 tr.append($("<th>").text("status"));
	 			 	 th.append(tr);
	 			 	 $("#success_table").append(th);
	 			 	 var tb = $("<tbody>");
    			  advertisement.forEach(function(item){
    				
					var tr = $("<tr>");
				   	tr.append($("<td>").text(++index));
    				 tr.append($("<td>").text(item.adPrice));
    			 	 tr.append($("<td>").text(item.foodName));
    			 	 tr.append($("<td>").text(item.foodPrice));
    			 	 tr.append($("<td>").text(getMyDate(item.startTime)));
    			 	 tr.append($("<td>").text(getMyDate(item.endTime)));
    			 	 tr.append($("<td>").text(item.status));
    			 	 tb.append(tr);
    			 	 $("#success_table").append(tb);
    			  })
    		  })
    	  });
    	
    });